package com.leo.manage_system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.UserWechat;
import com.leo.manage_system.entity.vo.UserVo;
import com.leo.manage_system.service.UserWechatService;
import com.leo.manage_system.utils.JwtUtils;
import com.leo.manage_system.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 微信用户信息 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/service_manage/user-wechat")

@Api(tags = "微信平台 用户管理")
@CrossOrigin
public class UserWechatController {
    JwtUtils jwtUtils = new JwtUtils();
    final UserWechatService userWechatService ;

    @Autowired
    public UserWechatController(UserWechatService userWechatService) {
        this.userWechatService = userWechatService;
    }
    @ApiOperation(value = "分页用户列表")
    @GetMapping("UserWechatPageQuery/{page}/{limit}")
    public R getUserPageQuery(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "resQuery", value = "查询", required = false)
            @RequestBody(required = false) UserVo userVo
    ){
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            Page<UserWechat> pageParam = new Page<>(page, limit);
            return R.ok().data(userWechatService.getUserWechatPageQuery(pageParam,userVo));
        }

    }

    @ApiOperation(value = "根据openID删除用户")
    @DeleteMapping("deleteUserWechat")
    public R deleteAdmin(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "openId", value = "openId", required = true)
            @RequestParam String openId){

        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = userWechatService.removeById(openId);
            if(!code) {
                return R.error().message("删除失败");
            }
            return R.ok().message("删除成功");
        }
    }
    @ApiOperation("根据openID编辑用户")
    @PostMapping("/modifyUserWechat")
    public R modifyRes(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam("用户") @RequestBody UserWechat userWechat) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = userWechatService.updateById(userWechat);
            if(!code) {
                return R.error().message("编辑失败");
            }
            return R.ok().message("编辑成功");
        }
    }
    @ApiOperation("获取近7天注册量 dayi表示第i天前的注册量")
    @PostMapping("/getUserRegistrations")
    public R getUserRegistrations(@ApiParam(name = "token", value = "token", required = true)
                                      @RequestParam(value="token") String token){
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            return R.ok().data("List",userWechatService.getUserRegistrations());
        }

    }

    @ApiOperation("获取近7天访问量 dayi表示第i天前的访问量")
    @PostMapping("/getUserAccess")
    public R getUserAccess(@ApiParam(name = "token", value = "token", required = true)
                               @RequestParam(value="token") String token){
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {

            return R.ok().data("List",userWechatService.getUserAccess());
        }

    }


}

